package com.yuan;

import com.yuan.bean.Pet;
import com.yuan.bean.Test;
import com.yuan.bean.User;
import com.yuan.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.interceptor.CacheAspectSupport;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication(scanBasePackages = "com")//SpringBoot应用
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan("com")
public class SpringBoot2Demo1Application {

    public static void main(String[] args) {
        //开启SpringBoot内嵌Tomcat  返回我们的IOC组件
        ConfigurableApplicationContext run = SpringApplication.run(SpringBoot2Demo1Application.class, args);

        //可以查看容器里面的组件
        String[] beanDefinitionNames = run.getBeanDefinitionNames(); //字符串数组集合
        //循环遍历
        for (String beanDefinitionName : beanDefinitionNames) {
            //打印数据结果
            System.out.println(beanDefinitionName);
        }

        //从容器中获取组件
        Pet tom = run.getBean("tom", Pet.class);
        //打印获取的组件
        System.out.println("组件：" + tom);
        //从容器中获取配置组件
        MyConfig bean = run.getBean(MyConfig.class);
        //打印获取的组件
        System.out.println(bean);


        /**
         * 如果@Configuration(proxyBeanMethods = true) 代理对象调用方法，SpringBoot总会检查这个组件是否在容器中有
         * 他要保证单实例
         */
        //使用配置类调用里面的方法
        User user = bean.user01();
        //二次获取相同对象
        User user1 = bean.user01();
        //进行相等运算
        System.out.println(user == user1);

        //通过容器id获取组件
        User user01 = run.getBean("user01", User.class);
        //判断用户引用的宠物是否是容器中的
        System.out.println("用户的宠物：" + (user01.getPet() == tom));


        //假设分割符
        System.out.println("------------------");

        //通过Type类型获取组件
        String[] beanNamesForType = run.getBeanNamesForType(User.class);
        //循环打印
        for (String s : beanNamesForType) {
            //打印结果
            System.out.println(s);
        }

        Test bean1 = run.getBean(Test.class);
        System.out.println(bean1);

        //判断容器中是否有这个Bean对象
        boolean test = run.containsBean("test");
        //打印结果
        System.out.println("容器中的Tom组件" + test);

        //获取Bean
        boolean user011 = run.containsBean("user01");
        //打印结果
        System.out.println(user011);

        //判断bean是否存在
        boolean haha = run.containsBean("haha");
        //打印结果
        System.out.println("哈哈：" + haha);
        //判断bean是否存在
        boolean hehe = run.containsBean("hehe");
        //打印结果
        System.out.println("呵呵：" + hehe);

        //获取SpringBoot中所有的组件
        int beanDefinitionCount = run.getBeanDefinitionCount();
        //打印总行数
        System.out.println(beanDefinitionCount);

        //通过类型为切面的获取有多少个
        String[] beanNamesForType1 = run.getBeanNamesForType(CacheAspectSupport.class);
        //打印行数
        System.out.println(beanNamesForType1.length);

        //查看容器中组件
        String[] beanNamesForType2 = run.getBeanNamesForType(WebMvcConfigurer.class);
        //打印组件
        System.out.println("========" + beanNamesForType2.length);
    }
}
